<?php

namespace App\Http\Controllers\Admin;

use App\Facade\AppContext;
use App\Http\Requests\Admin\ChangePasswordFormRequest;
use App\Models\Ca\Customer;
use App\Repositories\System\UserRepository;
use App\Utils\Output;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;

class IndexController extends BaseController
{
    public function __construct()
    {
        $this->set('index');
    }

    function index()
    {
        $pageName = '首页';
        $query = Customer::query()->where('hospital_id', '=', AppContext::hospitalId());

        $rd['a'] = (clone $query)->count();
        $rd['d'] = (clone $query)->where('created_at', '>=', date('Y-m-d'))->count();
        $rd['w'] = (clone $query)->where('created_at', '>=', Carbon::now()->startOfWeek())->count();
        $rd['m'] = (clone $query)->whereMonth('created_at', Carbon::now()->month)->count();
        return $this->view('index', compact('pageName', 'rd'));
    }

    function hospital(Request $request): Output
    {
        AppContext::hospitalId($request->get('hospital_id'));
        return Output::success('设置成功');
    }

    public function theme()
    {
        return $this->view('theme');
    }

    public function lock_screen()
    {
        return $this->view('lock_screen');
    }

    public function password()
    {
        return $this->view('password');
    }


    public function note()
    {
        return $this->view('note');
    }

    public function message()
    {
        return $this->view('message');
    }

    public function userInfo()
    {
        return $this->view('userInfo');
    }

    function menus()
    {
        return AppContext::menus();
    }

    function welcome()
    {
        return $this->view('welcome');
    }

    function baidu(Request $request)
    {
        $address = $request->get('address', '');
        $lng = $request->get('lng', '');
        $lat = $request->get('lat', '');
        return $this->view('baidu', compact('address', 'lng', 'lat'));
    }

    function changePassword(ChangePasswordFormRequest $request, UserRepository $repository)
    {
        return $repository->changePassword(AppContext::userId(), $request->get('old'), $request->get('password'));
    }

    function skin(Request $request, UserRepository $repository)
    {
        $repository->changeSkin(AppContext::userId(), $request->get('value'));
        $info = AppContext::userInfo();
        $info['skin'] = $request->get('value');
        AppContext::setUserInfo($info);
    }
}
